﻿using DevExpress.DirectX.Common.DirectWrite;
using Marmat.Forms.Views.Grid;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms
{
    /// <summary>
    /// 
    /// </summary>
    public static class GridControlExtensions
    {

        /// <summary>
        /// 开启删除列
        /// </summary>
        public static void OpenDeleteColumn(this Marmat.Forms.BaseGrid gridControl)
        {
            OpenDeleteColumn(gridControl, (e) =>
            {
                if (Marmat.Forms.MessageBox.Show("确定要删除此条记录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    gridControl.DeleteRow(e.RowHandle);
            });
        }

        /// <summary>
        /// 开启删除列
        /// </summary>
        public static void OpenDeleteColumn(this Marmat.Forms.BaseGrid gridControl, Action<MouseRowColEventArgs> action)
        {
            OpenDeleteColumn(gridControl, action, (row) => true);
        }

        /// <summary>
        /// 开启删除列
        /// </summary>
        public static void OpenDeleteColumn(this Marmat.Forms.BaseGrid gridControl, Action<MouseRowColEventArgs> action, Func<object, bool> condition)
        {
            if (!gridControl.Columns.Contains("delete"))
            {
                Marmat.Forms.Views.DataGrid.Column column = new Marmat.Forms.Views.DataGrid.Column();
                column.IsTemporary = true;
                column.Name = "delete";
                column.Caption = " ";
                column.Width = 28;
                gridControl.Columns.Add(column);
                gridControl.CustomDrawCell += (_, e) =>
                {
                    if (e.Column.Name == "delete" && condition.Invoke(e.Row))
                    {
                        e.DefaultDraw();
                        e.Handled = true;
                        Image img = Marmat.Template.WinForm.Properties.Resources.delete_icon;
                        e.Graphics.DrawImage(img, e.Bounds.X + (e.Bounds.Width - img.Width) / 2, e.Bounds.Y + (e.Bounds.Height - img.Height) / 2);
                    }
                };
                gridControl.ShowingEditor += (_, e) =>
                {
                    if (e.Column.Name == "delete")
                        e.Cancel = true;
                };
                gridControl.RowCellClick += (_, e) =>
                {
                    if (e.Column.Name == "delete" && e.Button == MouseButtons.Left && condition.Invoke(e.Row))
                    {
                        action.Invoke(e);
                    }
                };
            }
            else
                gridControl.Columns["delete"].Visible = true;
        }

        /// <summary>
        /// 关闭删除列
        /// </summary>
        public static void CloseDeleteColumn(this Marmat.Forms.GridControl gridControl)
        {
            if (gridControl.Columns.Contains("delete"))
                gridControl.Columns["delete"].Visible = false;
        }
    }
}
